package com.hzh.cusviewdemo.Util;

import android.content.Context;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.StringUtils;
import com.hzh.cusviewdemo.Music.Constant;
import com.hzh.cusviewdemo.Music.MyMusicUtil;
import com.hzh.cusviewdemo.R;
import com.kongzue.dialogx.dialogs.CustomDialog;
import com.kongzue.dialogx.dialogs.PopNotification;
import com.kongzue.dialogx.dialogs.PopTip;
import com.kongzue.dialogx.interfaces.DialogLifecycleCallback;
import com.kongzue.dialogx.interfaces.DialogXStyle;
import com.kongzue.dialogx.interfaces.OnBindView;

import java.util.List;
import java.util.Locale;


public class DialogUtils {

    /**
     * 获取APP语言
     *
     * @return
     */
    public static String getAppLanguage() {
        return Locale.getDefault().getLanguage();
    }

    /**
     * 获取PDA/Phone Code
     *
     * @return
     */
    public static String getDeviceCode() {
        return DeviceUtils.getUniqueDeviceId();
    }

    /**
     * 普通的吐司《好看》
     *
     * @param content
     */
    public static void showNormalToast(String content) {
        PopTip.show(content);
    }

    public static void showSuccessToast(int resId) {
        showSuccessToast(StringUtils.getString(resId));
    }

    /**
     * 成功的吐司《好看》
     *
     * @param content
     */
    public static void showSuccessToast(String content) {
        PopTip.show(content).iconSuccess();
    }

    public static void showErrorToast(int resId) {
        showErrorToast(StringUtils.getString(resId));
    }

    /**
     * 错误的吐司《好看》
     *
     * @param content
     */
    public static void showErrorToast(String content) {
        PopTip.show(content).iconError();
    }

    /**
     * 统一提示弹窗 按文本索引提示
     *
     * @param
     */
    public static void showErrorDialog(int stringId) {
        showErrorDialog(StringUtils.getString(stringId), null);
    }

    /**
     * 统一提示弹窗，带关闭回调
     */
    public static void showErrorDialog(int stringId, ErrorDialogDismissCallBack callBack) {
        showErrorDialog(StringUtils.getString(stringId), callBack);
    }

    public static void showErrorDialog(String content) {
        showErrorDialog(content, null);
    }

    /**
     * 统一提示弹窗，带关闭回调
     *
     * @param content
     */
    public static void showErrorDialog(String content, ErrorDialogDismissCallBack callBack) {
        CustomDialog.show(new OnBindView<CustomDialog>(R.layout.dialog_show_scan_tip) {
                    @Override
                    public void onBind(final CustomDialog dialog, View v) {
                        TextView dsst_tv_tip = v.findViewById(R.id.dsst_tv_tip);
                        TextView dsst_tv_done = v.findViewById(R.id.dsst_tv_done);
                        dsst_tv_tip.setText(content);
                        dsst_tv_done.setOnClickListener(v1 -> {
                            if (callBack != null) {
                                callBack.onDismiss();
                            }
                            dialog.dismiss();
                        });
                    }
                })
                .setDialogLifecycleCallback(new DialogLifecycleCallback<CustomDialog>() {
                    @Override
                    public void onDismiss(CustomDialog dialog) {
                        if (callBack != null) {
                            callBack.onDismiss();
                        }
                    }
                })
                .setMaskColor(Color.parseColor("#4D000000"));
    }

    /**
     * 最通用的对话框提示
     *
     * @param content
     * @param callBack
     */
    public static void showYesOrNoTipDialog(String content, YesOrNoDismissCallBack callBack) {
        showYesOrNoTipDialog("提示", content, callBack);
    }

    /**
     * 带有标题，取消和确认的Dialog，只需要传递显示的具体Tip内容
     *
     * @param content
     * @param callBack
     */
    public static void showYesOrNoTipDialog(String title, String content, YesOrNoDismissCallBack callBack) {
        showYesOrNoTipDialog(title, content, "取消", "确认", false, callBack);
    }

    public static void showYesOrNoTipDialog(String title, String content, String btn1Text, String btn2Text, YesOrNoDismissCallBack callBack) {
        showYesOrNoTipDialog(title, content, btn1Text, btn2Text, false, callBack);
    }

    public static void showYesOrNoTipDialog(String title, String content, String btn1Text, String btn2Text, boolean isCanClose, YesOrNoDismissCallBack callBack) {
        CustomDialog.show(new OnBindView<CustomDialog>(R.layout.dialog_yes_or_no_tip) {
                    @Override
                    public void onBind(final CustomDialog dialog, View v) {
                        TextView dynt_title = v.findViewById(R.id.dynt_title);
                        TextView dynt_tv_tip = v.findViewById(R.id.dynt_tv_tip);
                        TextView dynt_tv_cancel = v.findViewById(R.id.dynt_tv_cancel);
                        TextView dynt_tv_done = v.findViewById(R.id.dynt_tv_done);
                        dynt_title.setText(title);
                        dynt_tv_tip.setText(content);
                        dynt_tv_cancel.setText(btn1Text);
                        dynt_tv_done.setText(btn2Text);
                        if (!isCanClose) {
                            // 隐藏取消按钮
                            dynt_tv_cancel.setVisibility(View.GONE);
                        } else {
                            dynt_tv_cancel.setVisibility(View.VISIBLE);
                        }
                        dynt_tv_cancel.setOnClickListener(v1 -> {
                            if (callBack != null) {
                                callBack.onCancel(dialog);
                            }
                        });
                        dynt_tv_done.setOnClickListener(v12 -> {
                            if (callBack != null) {
                                callBack.onDone(dialog);
                            }
                        });
                    }
                })
                .setCancelable(isCanClose)
                .setMaskColor(Color.parseColor("#4D000000"));
    }

    /**
     * 秘钥校验dialog
     *
     * @param callBack
     */
    public static void showCheckKeyDialog(CheckKeyCallBack callBack) {
        CustomDialog.show(new OnBindView<CustomDialog>(R.layout.dialog_check_key) {
                    @Override
                    public void onBind(final CustomDialog dialog, View v) {
                        EditText dck_edit_key = v.findViewById(R.id.dck_edit_key);
                        TextView dck_device_code = v.findViewById(R.id.dck_device_code);
                        TextView dck_quit = v.findViewById(R.id.dck_quit);
                        TextView dck_check = v.findViewById(R.id.dck_check);
                        if (!MyMusicUtil.getStringShared(Constant.SAVE_USER_KEY).equals("")) {
                            dck_edit_key.setText(MyMusicUtil.getStringShared(Constant.SAVE_USER_KEY));
                        }
                        dck_device_code.setText("设备号: " + DialogUtils.getDeviceCode() + "\n开发者：黄宗豪\nPhone：18170014087");
                        dck_quit.setOnClickListener(v1 -> {
                            if (callBack != null) {
                                callBack.onQuit(dialog);
                            }
                        });
                        dck_check.setOnClickListener(v12 -> {
                            if (callBack != null) {
                                callBack.onCheck(dialog, dck_edit_key.getText().toString());
                            }
                        });
                    }
                })
                .setCancelable(false)
                .setMaskColor(Color.parseColor("#4D000000"));
    }


    public interface ErrorDialogDismissCallBack {
        void onDismiss();
    }

    public interface YesOrNoDismissCallBack {
        void onDone(CustomDialog dialog);

        void onCancel(CustomDialog dialog);
    }

    public interface CheckKeyCallBack {
        void onCheck(CustomDialog dialog, String key);

        void onQuit(CustomDialog dialog);
    }


    /**
     * 隐藏软键盘
     */
    public static void hideKeyboard(View view) {
        if (view == null) {
            return;
        }
        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (manager == null) {
            return;
        }
        manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 显示软键盘，需要先 requestFocus 获取焦点，如果是在 Activity Create，那么需要延迟一段时间
     */
    public static void showKeyboard(View view) {
        if (view == null) {
            return;
        }
        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (manager == null) {
            return;
        }
        manager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

}
